﻿#pragma execution_character_set("utf-8")

#include "Scene0913.h"
#include<string.h>

#include "Scene0910.h"

USING_NS_CC;

Scene* Scene0913::createScene()
{
	return Scene0913::create();
}
bool Scene0913::init() {
	if (!Scene::init())
	{
		return false;
	}

	auto visibleSize = Director::getInstance()->getVisibleSize();
	self_pos = 3;
	player_self = new Player();
	player_self->account = "自己";
	player_self->position = 3;
	loadPlayerUI(player_self);


	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("GameSence.plist", "GameSence.png");

	MenuItemFont *item1 = MenuItemFont::create("添加上家",
		CC_CALLBACK_1(Scene0913::menuItemClicked, this));
	item1->setScale(0.5);
	item1->setPosition(Vec2(-80, 0));
	item1->setTag(1);

	MenuItemFont *item2 = MenuItemFont::create("添加对家",
		CC_CALLBACK_1(Scene0913::menuItemClicked, this));
	item2->setScale(0.5);
	item2->setPosition(Vec2(0, 50));
	item2->setTag(2);

	MenuItemFont *item3 = MenuItemFont::create("添加下家",
		CC_CALLBACK_1(Scene0913::menuItemClicked, this));
	item3->setScale(0.5);
	item3->setPosition(Vec2(80, 0));
	item3->setTag(3);

	MenuItemImage *item4 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(Scene0913::menuItemClicked, this));
	item4->setTag(100);
	item4->setScale(0.5);
	item4->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	item4->setPosition(Vec2(-visibleSize.width / 2, visibleSize.height / 2));
	item4->setScale(2);

	MenuItemFont *item5 = MenuItemFont::create("开始游戏",
		CC_CALLBACK_1(Scene0913::begingame, this));
	item5->setScale(0.5);
	item5->setPosition(Vec2(0, -50));


	//判断选牌类型的按钮
	MenuItemFont* item6 = MenuItemFont::create("判断选牌类型",
		CC_CALLBACK_1(Scene0913::menuItemClicked, this));
	item6->setVisible(false);
	item6->setTag(200);

	Menu* menu = Menu::create(item1, item2, item3, item4, item5, item6, NULL);
	menu->setTag(300);
	this->addChild(menu);
	
	menuItemClicked(item1);
	menuItemClicked(item2);
	menuItemClicked(item3);
	item1->setVisible(false);
	item2->setVisible(false);
	item3->setVisible(false);

	//添加晒红十或者不晒
	Button* showButton = Button::create("show_normal.png");
	showButton->setPosition(Vec2(visibleSize.width / 2 - 50, visibleSize.height / 2));
	showButton->setVisible(false);
	showButton->setTag(100);
	showButton->addTouchEventListener(CC_CALLBACK_2(Scene0913::buttonClicked, this));
	addChild(showButton);

	Button* unshowButton = Button::create("unShow_normal.png");
	unshowButton->setPosition(Vec2(visibleSize.width / 2 + 50, visibleSize.height / 2));
	unshowButton->setVisible(false);
	unshowButton->setTag(200);
	unshowButton->addTouchEventListener(CC_CALLBACK_2(Scene0913::buttonClicked, this));
	addChild(unshowButton);


	EventListenerTouchOneByOne* linstener = EventListenerTouchOneByOne::create();
	linstener->onTouchBegan = CC_CALLBACK_2(Scene0913::onTouchBegan, this);
	linstener->onTouchMoved = CC_CALLBACK_2(Scene0913::onTouchMoved, this);
	linstener->setSwallowTouches(true);
	auto director = Director::getInstance()->getEventDispatcher();
	director->addEventListenerWithSceneGraphPriority(linstener, this);
	return true;
}

void Scene0913::buttonClicked(Ref* sender, Widget::TouchEventType type) {//亮牌按钮
	if (type == Widget::TouchEventType::ENDED)
	{
		Button* btn = (Button*)sender;
		switch (btn->getTag())
		{
		case 100: {

			int res = 0;
			if (game->red10_fk == 's'&&game->red10_hx == 's')
			{
				res = 3;
			}
			else if (game->red10_fk == 's')
			{
				res = 1;
			}
			else if (game->red10_hx == 's')
			{
				res = 2;
			}
			player_self->show(res);
			break;
		}
		case 200:

			break;
		default:
			break;
		}
		getChildByTag(100)->setVisible(false);
		getChildByTag(200)->setVisible(false);
		getChildByTag(300)->getChildByTag(200)->setVisible(true);
	}
}
void Scene0913::onTouchMoved(Touch* touch, Event* event) {
	Vec2 point = touch->getLocation();
	Vec2 start_location = touch->getStartLocation();

	if (std::abs(point.x - start_location.x)<30)
	{
		return;
	}
	if (!game)
	{
		return;
	}
	int count = player_self->countCards();

	for (int i = 0; i < count; i++)
	{
		//通过牌的值来获取牌的精灵
		Node* card_node = game->selfCards_node->getChildByTag(player_self->cards[i]);
		//获取该节点所占的区域
		Rect rect = card_node->getBoundingBox();
		Rect touchRect = Rect(rect.origin.x, rect.origin.y, (i == count - 1 ?
			rect.size.width : 30), rect.size.height);
		if (touchRect.containsPoint(point))
		{
			if (card_node->getPositionY() == cardsOriginY)
			{
				card_node->setPositionY(cardsOriginY + 10);
			}
			else
			{
				card_node->setPositionY(cardsOriginY);
			}

		}
	}

}
bool Scene0913::onTouchBegan(Touch* touch, Event* event) {
	Vec2 point = touch->getLocation();
	//前提有牌
	if (!game)
	{
		return true;
	}
	int count = player_self->countCards();
	bool didTouchCard = false;
	for (int i = 0; i < count; i++)
	{
		//通过牌的值来获取牌的精灵
		Node* card_node = game->selfCards_node->getChildByTag(player_self->cards[i]);
		//获取该节点所占的区域
		Rect rect = card_node->getBoundingBox();
		Rect touchRect = Rect(rect.origin.x, rect.origin.y, (i == count - 1 ?
			rect.size.width : 30), rect.size.height);
		if (touchRect.containsPoint(point))
		{
			if (card_node->getPositionY() == cardsOriginY)
			{
				MoveTo* moveto = MoveTo::create(0.5f, Vec2(card_node->getPositionX(), cardsOriginY + 10));
				card_node->runAction(moveto);
				card_node->setPositionY(cardsOriginY + 10);
			}
			else
			{
				card_node->setPositionY(cardsOriginY);
			}
			didTouchCard = true;
			break;
		}
	}
	if (!didTouchCard)
	{
		for (int i = 0; i < count; i++)
		{
			Node* card_node = game->selfCards_node->getChildByTag(player_self->cards[i]);
			card_node->setPositionY(cardsOriginY);
		}
	}
	return true;
}
void Scene0913::begingame(Ref* sender) {
	auto visibleSize = Director::getInstance()->getVisibleSize();
	if (player_self && player_right && player_front && player_left) {
	}
	else {
		return;
	}
	//初始化的清理工作
	if (game)
	{
		game->selfCards_node->removeAllChildrenWithCleanup(true);
		player_self->node->removeChildByTag(2);
		CC_SAFE_DELETE(game);
	}
	//log("游戏开始：洗牌～～～");
	// 1.洗牌
	int cards[54];
	game = new CardGame();
	//game->isgame = true;
	game->shuffle(cards, 54);

	// 2.发牌
	player_self->initCards();
	player_right->initCards();
	player_front->initCards();
	player_left->initCards();
	int start_pos = 3; // 先发给3号椅子的人
	Player *currentPlayer;
	for (int i = 0; i < 54; i++) {
		char pos_char = getPlayerCharPosition((start_pos + i - 1) % 4 + 1);

		// 3
		if (cards[i] == 41) {
			game->first = pos_char;
		}
		// 10
		if (cards[i] == 22) {
			game->red10_fk = pos_char;
		}
		// 10
		if (cards[i] == 48) {
			game->red10_hx = pos_char;
		}

		// 把当前遍历的牌给到对应的玩家
		if (pos_char == 's') {
			currentPlayer = player_self;
		}
		else if (pos_char == 'l') {
			currentPlayer = player_left;
		}
		else if (pos_char == 'f') {
			currentPlayer = player_front;
		}
		else if (pos_char == 'r') {
			currentPlayer = player_right;
		}
		currentPlayer->cards[i / 4] = cards[i];
	}
	//发牌结束
	if (game->red10_fk == 's' || game->red10_hx == 's')
	{
		getChildByTag(100)->setVisible(true);
		getChildByTag(200)->setVisible(true);
	}
	else
	{

		getChildByTag(100)->setVisible(false);
		getChildByTag(200)->setVisible(false);
		getChildByTag(300)->getChildByTag(200)->setVisible(true);
	}
	// 3.展现自己的牌
	printf("自己的牌：");

	// 排序
	player_self->sortCards();

	int count = player_self->countCards();
	int mid = count / 2;
	float midx = (visibleSize.width + 150) / 2;//纸牌组的X中心位置
											   // 显示
	for (int i = 0; i < count; i++) {

		Sprite* cardSprite = game->getCardSpriteWithCardValue(player_self->cards[i]);
		cardSprite->setScale(0.8f);
		cardSprite->setPosition(Vec2(midx + 20 * (i - mid) + (count % 2 == 0 ? 15 : 0),
			cardSprite->getBoundingBox().size.height / 2 + 10));
		cardsOriginY = cardSprite->getPositionY();
		game->selfCards_node->addChild(cardSprite);

	}
	addChild(game->selfCards_node);

}
void Scene0913::menuItemClicked(Ref* sender) {
	MenuItemFont* item = (MenuItemFont*)sender;

	switch (item->getTag()) {
	case 1: {// 在左侧添加玩家
		player_left = new Player();
		player_left->account = "左边";
		player_left->position = 2;
		loadPlayerUI(player_left);

		item->setString("移除左侧玩家");
		item->setTag(4);
		break;
	}
	case 2: {// 在对面添加玩家
		player_front = new Player();
		player_front->account = "对面";
		player_front->position = 1;
		loadPlayerUI(player_front);

		item->setString("移除对面玩家");
		item->setTag(5);
		break;
	}
	case 3: {// 在右侧添加玩家
		player_right = new Player();
		player_right->account = "右边";
		player_right->position = 4;
		loadPlayerUI(player_right);

		item->setString("移除右侧玩家");
		item->setTag(6);
		break;
	}
	case 4: {// 从左侧移除玩家
		player_left->node->removeAllChildrenWithCleanup(true);
		CC_SAFE_DELETE(player_left);

		item->setString("添加左侧玩家");
		item->setTag(1);
		break;
	}
	case 5: {// 从对面移除玩家
		player_front->node->removeAllChildrenWithCleanup(true);
		CC_SAFE_DELETE(player_front);

		item->setString("添加对面玩家");
		item->setTag(2);
		break;
	}
	case 6: {// 从右侧移除玩家
		player_right->node->removeAllChildrenWithCleanup(true);
		CC_SAFE_DELETE(player_right);

		item->setString("添加右侧玩家");
		item->setTag(3);
		break;
	}
	case 200: {//判断选牌类型的按钮
		vector<int> arr;
		for (int i = 0; i < player_self->countCards(); i++)
		{
			Node* card_node = game->selfCards_node->getChildByTag(player_self->cards[i]);
			if (card_node->getPositionY()>cardsOriginY)
			{
				arr.push_back(player_self->cards[i]);
			}
		}
		PokerType type = game->getPokerTypeWithCards(arr);
		switch (type)
		{
		case POKER_TYPE_SINGLE: {
			log("单牌");
			break;
		}
		case POKER_TYPE_DOUBLE: {
			log("对子");
			break;
		}
		case POKER_TYPE_KING_BOMB: {
			log("王炸");
			break;
		}
		case POKER_TYPE_REDTEN_BOMB: {
			log("红十炸");
			break;
		}
		case POKER_TYPE_THREE_BOMB: {
			log("炸弹（3）");
			break;
		}
		case POKER_TYPE_FOUR_BOMB: {
			log("炸弹（4）");
			break;
		}
		case POKER_TYPE_STRAIGHT: {
			log("顺子");
			break;
		}
		default:
			log("牌型有误！");
			break;
		}
	}
	default:
		//Director::getInstance()->replaceScene(Scene0910::createScene());
		break;
	}
}
void Scene0913::loadPlayerUI(Player* player) {
	//获取该玩家在界面的何处
	
	player->pos_char = getPlayerCharPosition(player->position);

	player->createPlayerNode();
	addChild(player->node);
}
char Scene0913::getPlayerCharPosition(int pos) {
	if (self_pos == 4)
	{
		if (pos == 1)
		{
			return 'r';
		}
		if (pos == 2)
		{
			return 'f';
		}
		if (pos == 3)
		{
			return 'l';
		}
	}
	else if (self_pos == 1)
	{
		if (pos == 2)
		{
			return 'r';
		}
		if (pos == 3)
		{
			return 'f';
		}
		if (pos == 4)
		{
			return 'l';
		}
	}
	else if (self_pos == 2)
	{
		if (pos == 1)
		{
			return 'l';
		}
		if (pos == 3)
		{
			return 'r';
		}
		if (pos == 4)
		{
			return 'f';
		}
	}
	else if (self_pos == 3)
	{
		if (pos == 1)
		{
			return 'f';
		}
		if (pos == 2)
		{
			return 'l';
		}
		if (pos == 4)
		{
			return 'r';
		}
	}
	return 's';

}